<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: groups.php Sat Jul 18 13:13:35 CST 2009
 */

if ( !defined("XOOPS_ROOT_PATH") ) exit; 
if ( !class_exists("xoGroups") ) {
    include dirname(dirname(__FILE__))."/include/groups.php";
}
class groupsGroup extends XoopsObject 
{
    public function __construct() 
    {
    	$this->initVar("group_id", XOBJ_DTYPE_INT, 0, false);
    	$this->initVar("uid", XOBJ_DTYPE_INT, null, true);
    	$this->initVar("cat_id", XOBJ_DTYPE_INT, null, true);
    	$this->initVar("group_creator", XOBJ_DTYPE_TXTBOX, null, true);
    	$this->initVar("group_name", XOBJ_DTYPE_TXTBOX, null, true);
    	$this->initVar("group_logo", XOBJ_DTYPE_TXTBOX, null, false);
    	$this->initVar("group_description", XOBJ_DTYPE_TXTAREA, null, true);
    	$this->initVar("group_notice", XOBJ_DTYPE_TXTBOX, null, false);
    	$this->initVar("group_create_time", XOBJ_DTYPE_INT, null, false);
    	$this->initVar("group_status", XOBJ_DTYPE_INT, 0, false);
    
    	$this->initVar("group_topic_total", XOBJ_DTYPE_INT, 0, false);
    	$this->initVar("group_reply_total", XOBJ_DTYPE_INT, 0, false);
    	$this->initVar("group_member_total", XOBJ_DTYPE_INT, 0, false);		
    	$this->initVar("group_foot_total", XOBJ_DTYPE_INT, 0, false); 
    
    	$this->initVar("group_member_uids", XOBJ_DTYPE_ARRAY, 0, false); 
    	$this->initVar("group_footprint_uids", XOBJ_DTYPE_ARRAY, 0, false); 
    }
	
	public function id() {
	    return (int)$this->getVar("group_id","n");
	}
	
	public function uid() {
	    return (int)$this->getVar("uid","n");
	}
	
	public function catId() {
	    return (int)$this->getVar("cat_id","n");
	}
	
	public function author() {
	    return $this->getVar("group_creator","n");
	}
	
	public function name() {
	    return $this->getVar("group_name","n");
	}
	
	public function logo() {
	    return $this->getVar("group_logo") ? $this->getVar("group_logo","n") : "group.gif";
	}
	
	public function description($format="s") {
	    return $this->getVar("group_description",$format);
	}
	
	public function status() {
	    return $this->getVar("group_status") ? true : false;
	}
	
	public function statusDesc() {
	    return $this->getVar("group_status") ? $this->getVar("group_status") : "__";
	}
	
	public function notice() {
	    if ( $this->getVar("group_notice") ) {
	        return $this->getVar("group_notice","n");
	    }
	    return;
	}
	
	public function admin() {
	    global $xoopsUser;
	    if ( empty($xoopsUser) || !is_object($xoopsUser) ) return false;
	    if ( $xoopsUser->uid() === $this->getVar("uid","n") ) return true;
	    return false;
	}
	
	public function userjoin($group_id) {
	    global $xoopsUser;
	    if ( empty($xoopsUser) || !is_object($xoopsUser) ) {
	        return ;
	    }
	    
	    $ret = '<a href="javascript:%s('.$group_id.')" > %s </a>';
	    if ( in_array($xoopsUser->uid(),$this->getVar("group_member_uids")) ) {
	        return sprintf($ret,'removeGroup',_GP_GROUP_LOGOUT);
	    }
	    return sprintf($ret,'joinGroup',_GP_GROUP_JOIN);
	}
	
	public function userfootprint($group_id) {
	    global $xoopsUser;
	    if ( empty($xoopsUser) || !is_object($xoopsUser) ) {
	        return ;
	    }
	    $ret = '<a href="javascript:%s('.$group_id.')" > %s </a>';
	    if ( in_array($xoopsUser->uid(),$this->getVar("group_footprint_uids")) ) {
	        return ;
	    }
	    return sprintf($ret,'accessGroup','到此一游');
	}
	
	public function createTime($format="l") {
		return formatTimestamp($this->getVar("group_create_time"),$format);
	}	
	
	public function info() {
	    return array(
            'id' => $this->id(),
            'name' => $this->name(),
            'uid' => $this->uid(),
            'catid' => $this->catId(),
            'status' => $this->status(),
            'description' => $this->description('n'),
            'userjoin' => $this->userjoin($this->id()),
            'userfootprint' => $this->userfootprint($this->id()),
            'notice' => $this->notice(),
            'members' => $this->getVar('group_member_total'),
            'topics' => $this->getVar('group_topic_total'),
            'replies' => $this->getVar('group_reply_total'),
            'admin' => $this->admin(),
        );
	}
}

class groupsGroupHandler extends XoopsPersistableObjectHandler
{
    public function __construct($db) {
        parent::__construct($db,"groups_group","groupsGroup","group_id");
    }
    
    /**
     * create and modify a group
     *
     * @param array $params
     * @return int $group_id
     */
    public function setGroup( array $params ) {
        $myts = MyTextSanitizer::getInstance();
        if ( empty($params) || !is_array($params) ) return false;
    	$group_id = isset($params["group_id"]) ? intval($params["group_id"]) : 0 ;
        if ( !empty($group_id) ) {
            $obj = $this->get($group_id);
        } else {
            $obj = $this->get();
            $obj->setVar("group_create_time",time());
        }
    	foreach ( $params as $key => $val ) {
	       if ( !isset($obj->vars[$key]) || $myts->stripSlashesGPC($val) == $obj->getVar($key) ) continue;
    	   $obj->setVar($key, $val);
    	}
    	if ( $group_id = $this->insert($obj) ) {
    	    return $group_id;
    	}
    	return false;
    }
    
    /**
     * get group data
     * 
     * @param int $group_id
     * @param int $asObj
     * @return object / array $ret
     */
    public function getGroup( $group_id, $asObj=true ) {
        $obj = $this->get($group_id);
        return $obj;
    }
    
    /**
     * update group field
     *
     * @param array $params
     * @return $group_id
     */
    public function updateGroupField( array $params, $asIncrease=true ) {
        extract($params);
        if ( empty($group_id) ) return false;
        $obj = $this->get(intval($group_id));
        if ( empty($obj) || !is_object($obj) || $obj->isNew() ) return false;
        if ( $asIncrease === true ) {
            $value = (int)$obj->getVar($field,"n") + intval($value);
        } 
        $obj->setVar($field,$value);
        if ( $this->insert($obj) ) {
            return $group_id;
        }
        return false;
    }
    
    /**
     * update this group is logo
     * $params = array("group_id"=>1,"value"=>"")
     * @param array $params
     * @return int $group_id
     */
    public function updateGroupLogo( array $params ) {
        extract($params);
        unset($params);
        $group_id = isset($group_id) ? intval($group_id) : 0;
        $params = array(
            "group_id"=>$group_id,
            "field"=> "group_logo",
            "value"=> trim($value),
            );
        return $this->updateGroupField($params,false);
    }
       
    /**
     * update this group is member total
     *
     * @param array $params
     * @return int $group_id
     */
    public function updateMemberTotal( array $params ) {
        extract($params);
        unset($params);
        $group_id = isset($group_id) ? intval($group_id) : 0;
        $params = array(
            "group_id"=>$group_id,
            "field"=> "group_member_total",
            "value"=> trim($value),
            );
        return $this->updateGroupField($params);
    }
    
    /**
     * update this group is topic total
     *
     * @param array $params
     * @return int $group_id
     */
    public function updateTopicTotal( array $params ) {
        extract($params);
        unset($params);
        $group_id = isset($group_id) ? intval($group_id) : 0;
        $params = array(
            "group_id"=>$group_id,
            "field"=> "group_topic_total",
            "value"=> trim($value),
            );
        return $this->updateGroupField($params);
    }
    
    /**
     * update this group is post total
     *
     * @param array $params
     * @return int $group_id
     */
    public function updateReplyTotal( array $params ) {
        extract($params);
        unset($params);
        $group_id = isset($group_id) ? intval($group_id) : 0;
        $params = array(
            "group_id"=>$group_id,
            "field"=> "group_reply_total",
            "value"=> trim($value),
            );
        return $this->updateGroupField($params);
    }
    
    /**
     * update this group footprint total
     *
     * @param int $group_id
     * @param int $val
     * @return int $group_id
     */
    public function updateFootTotal( $group_id, $val=1 ) {
        extract($params);
        unset($params);
        $group_id = isset($group_id) ? intval($group_id) : 0;
        $params = array(
            "group_id"=>$group_id,
            "field"=> "group_foot_total",
            "value"=> trim($value),
            );
        return $this->updateGroupField($params);
    }
    
    /**
     * update group member uids
     *
     * @param int $group_id
     * @param int $uid
     * @return boot
     */
    public function updateMemberUids( $group_id, $uid, $asJoin=true ) {
        if ( empty($group_id) || empty($uid) ) return false;
        $obj = $this->get($group_id);
        $uids = array();
        if ( $obj->getVar("group_member_uids") ) {
            $uids = $obj->getVar("group_member_uids");
        }
        if ( $asJoin === true ) {
            $uids[] = $uid;
        } else {
            foreach ( $uids as $k=>$val ) {
                if ( $val == $uid ) {
                    unset($uids[$k]);
                }
            }
        }
        $uids = array_unique($uids);
        $obj->setVar("group_member_uids",$uids);
        if( $this->insert($obj) ) {
            return true;
        }
        return false;
    }
    
    /**
     * update group footprint uids
     *
     * @param int $group_id
     * @param int $uid
     * @return bool
     */
    public function updateFootprintUids( $group_id, $uid ) {
        if ( empty($group_id) || empty($uid) ) return false;
        $obj = $this->get($group_id);
        $uids = array();
        if ( $obj->getVar("group_footprint_uids") ) {
            $uids = $obj->getVar("group_footprint_uids");
        }
        $uids[] = $uid;
        $uids = array_unique($uids);
        $obj->setVar("group_footprint_uids",$uids);
        if( $this->insert($obj) ) {
            return true;
        }
        return false;
    }
    /**
     * create a group cache file
     * 
     * @param int $group_id
     * @return bool
     *
     */
    public function updateGroupCache( $group_id ) {
        
    }
    
    /**
     * get my groups 
     *
     * @param int $uid
     * @param bool $asObj
     * @return array array/object array
     */
    public function getUserGroups( $uid, $asObj=true ) {
        
    }
    
    /**
     * Enter description here...
     *
     * @param int $group_id
     * @param int $num
     * @param int $asObj
     * @return array $ret array/object array
     */
    public function getGroupToGroups( $group_id, $num=10, $asObj=true ) {
        
    }
    
    /**
     * for select group
     *
     * @param array $params
     * @param bool $asObj
     * @return array $ret array/object array
     */
    public function getGroups( array $params, $asObj=true ) {
        $criteria = xoGroups::getCriteria($params);
        $fields = ( isset($params["fields"]) && is_array($params["fields"]) ) ? $params["fields"] : null ;
        $ret = $this->getAll($criteria, $fields, $asObj);
        return $ret;
    }

    /**
     * for footprint page nav
     *
     * @param array $params
     * @return string $pagenav
     */
    public function getPageNav( array $params ) {
        $pagenav = "";
        $criteria = xoGroups::getCriteria($params);
        $counts = $this->getCount($criteria);
        $extra = isset($params["extra"]) ? trim($params["extra"]) : "";
        if( $counts > $params["limit"]){	
        	$nav = new XoopsPageNav($counts, $params["limit"], $params["start"], "start",$extra);
        	$pagenav = $nav->renderNav(4);
        }
        return $pagenav;
    }
 }    
